/**
 * TrungTT
 */
package vn.com.tiz.DocumentZ.view;

import java.util.ArrayList;

import vn.com.tiz.DocumentZ.R;
import vn.com.tiz.bookcase.adapter.ChapterAdapter;
import vn.com.tiz.bookcase.entry.ChapterInfo;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class BookPageView extends LinearLayout{

	public static int MaxLine = 32;
	public static int LineHeight;
	
	String content;
	public TextView txvContent;
	Context context;
	
	public BookPageView(Context context) {
		super(context);
		this.context = context;
		// TODO Auto-generated constructor stub
		setBackgroundColor(Color.GRAY);
		setPadding(5, 0, 5, 0);
		setOrientation(LinearLayout.VERTICAL);
		
		txvContent = new TextView(context);
		txvContent.setTextColor(Color.BLACK);
		txvContent.setBackgroundColor(Color.WHITE);
		txvContent.setPadding(5, 10, 5, 10);
		//txvContent.setGravity(Gravity.CENTER_VERTICAL);
		
		LayoutParams lp = new  LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		txvContent.setLayoutParams(lp);
		//txvContent.getp
		addView(txvContent);
		LineHeight = txvContent.getLineHeight();
	}
	
	public void setupIndexPage(ArrayList<ChapterInfo> chapterList){
	
		TextView txvCategory = new TextView(context);
		txvCategory.setText(context.getString(R.string.category_text));		
		addView(txvCategory);
		LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
		txvCategory.setLayoutParams(lp1);
		txvCategory.setGravity(Gravity.CENTER);
		txvCategory.setTextColor(Color.BLACK);
		txvCategory.setTextSize(20);
		
		
		ListView lsvChapter = new ListView(context);
		setBackgroundColor(Color.WHITE);
		txvContent.setVisibility(View.GONE);
		setPadding(35, 20, 35, 20);
		setGravity(Gravity.CENTER);		
		//lsvChapter.set
		addView(lsvChapter);
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		lsvChapter.setLayoutParams(lp);
		ChapterAdapter adapter = new ChapterAdapter(context,chapterList);
		lsvChapter.setAdapter(adapter);
		lsvChapter.setBackgroundColor(Color.WHITE);
		lsvChapter.setDivider(null);
		lsvChapter.setDividerHeight(5);
		lsvChapter.setFadingEdgeLength(0);
	}
	
	public void setFontSize(float size){
		txvContent.setTextSize(size);
		LineHeight = txvContent.getLineHeight();
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		//Log.i("TrungTT","onlayout:"+b);
	}
	
	public int getTextHeight(){
		return txvContent.getLineHeight();
	}
		
	public int getHeightOfPage(){
		return txvContent.getHeight();
	}
	
	public int getMaxLineDisplay(){
		return 0;
	}
	
	@Override
	public void setBackgroundResource(int resid) {
		// TODO Auto-generated method stub
		super.setBackgroundResource(resid);
		txvContent.setVisibility(View.GONE);
	}
	
	public void loadContent(String value){
		content = value;
		txvContent.setText(content);
//		if(txvContent.getLineCount() >= (MaxLine - 5)){
//			txvContent.setGravity(Gravity.CENTER_VERTICAL);
//		}else{
//			txvContent.setGravity(Gravity.LEFT);
//		}
	}
	
	public float getWidthofString(String str){
		float reval = txvContent.getPaint().measureText(str);
		return reval;
	}
	
	
}
